package org.example.chat.websocket.messagehandler;

import org.example.chat.websocket.entity.MessagePack;
import org.example.chat.dao.ChatMessageRepository;
import org.example.chat.utils.SpringUtils;
import org.springframework.stereotype.Component;

/**
 * 签收消息处理器
 * @author cheval
 */
@Component
public class SignMessageHandler implements MessageHandler {
    @Override
    public void handler(MessageHandlerContext messageHandlerContext) {
        // 1. 获取要签收的消息id, 存放在扩展字段中，多个消息id以,隔开
        MessagePack messagePack = messageHandlerContext.messagePack;
        String messageIdStr = messagePack.getExtendField();
        if (messageIdStr == null || messageIdStr.trim().equals("")) {
            return;
        }
        String[] messageIds = messageIdStr.split(",");
        // 2. 批量签收，修改数据库中消息的状态为已签收 1
        // 获取ChatMessageRepository 的bean对象
        ChatMessageRepository repository = SpringUtils.getApplicationContext().getBean("chatMessageRepository", ChatMessageRepository.class);
        repository.batchUpdateSignFlag(messageIds);
    }
}
